<template>
    <el-container>
      <!-- 头部区域 -->
        <el-header>
          <div>
            <img src="../assets/logo_700jnzzy_1024@2x.png" alt="">
            <span>电商后台管理系统</span>
          </div>
          <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <!-- 页面主题区域 -->
        <el-container>
          <!-- 侧边栏 -->
          <el-aside :width="isCollapse ? '64px' : '200px'">
            <div class="toggle-button" @click="toggleCollapse">
              |||
            </div>
            <!-- 侧边栏菜单区 -->
            <el-menu
              background-color="#333744"
              text-color="#fff"
              active-text-color="#409eff"
              unique-opened
              :collapse="isCollapse"
              :collapse-transition="false"
              router
              :default-active="activePath"
              >
              <!-- 一级菜单 -->
              <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
                <!-- 一级菜单的模板区域 -->
                <template slot="title">
                  <!-- 图表 -->
                  <i :class="iconsObj[item.id]"></i>
                  <!-- 文本 -->
                  <span>{{item.authName}}</span>
                </template>

                <!-- 二级菜单 -->
                <el-menu-item :index="subItem.id === demoId ?'':`/${subItem.path}`" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState(`/${subItem.path}`,subItem.id)">
                  <template slot="title">
                    <!-- 图表 -->
                    <i class="el-icon-menu"></i>
                    <!-- 文本 -->
                    <span>{{subItem.authName}}</span>
                  </template>
                </el-menu-item>
              </el-submenu>
            </el-menu>
          </el-aside>
          <!-- 右侧主题内容 -->
          <el-main>
            <!-- 路由占位符 -->
            <router-view></router-view>
          </el-main>
        </el-container>

    <!-- 修改分类的对话框 -->
    <el-dialog
      title="请输入密码进入"
      :visible.sync="secretDialogVisble"
      width="40%"
      >
      <!-- 主题区域 -->
      <el-form :model="secretForm" :rules="secretFormRules" ref="secretFormRef" label-width="70px">
        <el-form-item label="密码" prop="password">
          <el-input v-model="secretForm.password"></el-input>
        </el-form-item>
      </el-form>
      <!-- 页脚按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="secretDialogVisble = false">取 消</el-button>
        <el-button type="primary" @click="goSecret">确 定</el-button>
      </span>
    </el-dialog>
    </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false, // 是否折叠
      activePath: '', // 被激活的链接地址
      demoId: 147,
      secretDialogVisble: false,
      secretForm: {
        password: ''
      },
      secretFormRules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      // 清除token
      window.sessionStorage.clear()
      // 返回登录页
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      res.data[4].children[1] = {
        authName: '小样',
        children: Array(0),
        id: 147,
        order: null,
        path: 'demo'
      }
      this.menulist = res.data
    },
    // 点击按钮切换折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath, id) {
      if (id === 147 && this.demoId === 147) {
        this.secretDialogVisble = true
      }
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    goSecret() {
      if (this.secretForm.password !== '8848') {
        return this.$message.error('密码错误！')
      }
      this.demoId = 2000
      this.$message.success('密码正确')
      this.secretDialogVisble = false
    }
  },
  mounted() {}
}
</script>

<style lang="less" scoped>
  .el-container{
    height: 100%;
  }

  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    div{
      display: flex;
      align-items: center;
      margin-left: 15px;
      span{
        margin-left: 15px;
        color: #fff;
      }
      img{
      width: 50px;
      height: 50px;
    }
    }

  }

  .el-aside{
    background-color: #333744;
    .el-menu{
      border-right: 0;
    }
  }

  .el-main{
    background-color: #eaedf1;
  }

  .iconfont{
    margin-right: 10px;
  }
  .toggle-button{
    background-color: #4a5064;
    color: #fff;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>
